<template>
  <div class="">
    <el-button type="success" size="small" @click="toAdd">添加</el-button>
    <!-- 表格 -->
    <div class="table">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          fixed
          prop="id"
          label="编号"
          width="150"
          align="center"
        />
        <el-table-column
          prop="username"
          label="管理员名称"
          width="120"
          align="center"
        />
      
        <el-table-column
          prop="icon"
          label="头像"
          width="120"
          align="center"
        >    <template #default="scope">
      <div style="display: flex; align-items: center">
          <el-image :src="scope.row.icon"/>
      </div>
  </template></el-table-column>
    

        <el-table-column
          prop="email"
          label="邮箱"
          width="120"
          align="center"
        />
        <el-table-column
          prop="nickName"
          label="昵称"
          width="120"
          align="center"
        />
        <el-table-column
          prop="note"
          label="备注信息"
          width="120"
          align="center"
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="120"
          align="center"
        />
        <el-table-column
          prop="loginTime"
          label="登入时间"
          width="120"
          align="center"
        />
        <el-table-column
          prop="status"
          label="启用状态"
          width="120"
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />

    <!-- 表单 -->
    <el-dialog v-model="dialogFormVisible" title="添加/编辑">
      <el-form :model="info">
        <el-form-item label="管理员名称" :label-width="formLabelWidth">
          <el-input v-model="info.username" autocomplete="off" />
        </el-form-item>

        <el-form-item label="头像" :label-width="formLabelWidth">
          <el-input v-model="info.icon" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="info.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="info.nickName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="备注信息" :label-width="formLabelWidth">
          <el-input v-model="info.note" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth">
          <el-input v-model="info.createTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="登入时间" :label-width="formLabelWidth">
          <el-input v-model="info.loginTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="启用状态" :label-width="formLabelWidth">
          <el-input v-model="info.statuus" autocomplete="off" />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button  @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="excute()"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
//模块引入
import info from "../../http/admininfo";
import { defineComponent } from "vue";

export default defineComponent({
  data() {
    return {
      tableData: [],
      page: 1,
      total: 0,
      limit: 10,
      info: {},
      dialogFormVisible: false,
    };
  },

  methods: {
    //分页查询
    fetchData() {
      //封装分页对象
      const vo = { current: this.page, size: this.limit };
      info.getinfoByPage(vo).then((res) => {
        console.log(res)
        //得到page对象
        const pageInfo = res.data.page;
        this.tableData = pageInfo.records;
        const src=this.tableData.total
        this.total = pageInfo.total;
      });
    },
     //删除业务 id作参
    handleDelete(id) {
      //封装数据对象
      //const data = { id };
      ElMessageBox.confirm("你确定删除该行数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          info.deleteById(id).then((res) => {
            if (res.success) {
              ElMessage({
                type: "success",
                message: "删除成功",
              });
              //刷新页面
              this.fetchData();
            } else {
              ElMessage({
                type: "error",
                message: "删除失败",
              });
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "warning",
            message: "取消删除",
          });
        });
    },

    //编辑-数据回显
    handleEdit(id) {
      //唤出编辑表单
      this.dialogFormVisible = true;
      //根据id查询 完成数据回显
      info.queryById(id).then((res) => {
        this.info = res.data.help;
      });
    },
    toAdd() {
      this.dialogFormVisible = true;
      this.info = {}
    },
    //执行编辑业务
    excute() {
      if (this.info.id != undefined) {
        //编辑逻辑
        info.updateById(this.info).then((res) => {
          if (res.success) {
            ElMessage({
              type: "success",
              message: "编辑成功",
            });
            //刷新页面
            this.fetchData();
          } else {
            ElMessage({
              type: "error",
              message: "编辑失败",
            });
          }
        });
        //关闭弹窗
        this.dialogFormVisible = false;
      } else {
        //添加逻辑
        info.addOne(this.info).then(res => {
          if(res.success){
            ElMessage({
              type: "success",
              message: "编辑成功",
            });
            //刷新页面
            this.fetchData();
          }else{
            ElMessage({
              type: "error",
              message: "添加失败",
            });
          }
          this.dialogFormVisible = false;
        })
      }
    },
    //更新每页显示数
    changeSize(size) {
      this.limit = size;
      this.fetchData();
    },
  },
  created() {
    this.fetchData(1);
  },
});
</script>

<style lang="scss" scoped>
.table {
  margin-top: 10px;
}
</style>